/*
Copyright (C) Ariff Ehsan, Abdul Hadi Fikri

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*  Author : Ariff Ehsan
 *  Date   : June 28, 2009
 */


#include <system.h>

uint8_t app_count;
BOOL app_state;

timer_t app_timer;
timer_t app_timer_motor;

void APP_TIMER_run(void);
void APP_TIMER_MOTOR_run(void);

void APP_booted(){
	
	/* initialize leds */
	LEDS8_init();
	
	app_count = 0;
	
	/* wire timer */
	TIMER_reg(&app_timer, APP_TIMER_run);
	TIMER_startPeriodic(&app_timer, 500);
	
	TIMER_reg(&app_timer_motor, APP_TIMER_MOTOR_run);
	TIMER_startPeriodic(&app_timer_motor, 10000);
	
	/* initialize motor */
	MOTOR_init();
	
	/* set initial motor speed and direction */
	MOTOR_setDir(MOTOR_RIGHT, MOTOR_FORWARD);
	MOTOR_setSpeed(MOTOR_RIGHT, 500);
	
	MOTOR_setDir(MOTOR_LEFT, MOTOR_BACKWARD);
	MOTOR_setSpeed(MOTOR_LEFT, 500);
	
}

void APP_TIMER_run(void) {
	app_count = ~app_count;
	LEDS8_set(app_count);
}

void APP_TIMER_MOTOR_run(void) {
	
	if (app_state) {
		MOTOR_setDir(MOTOR_RIGHT, MOTOR_FORWARD);
		MOTOR_setDir(MOTOR_LEFT, MOTOR_BACKWARD);
	}
	else {
		MOTOR_setDir(MOTOR_RIGHT, MOTOR_BACKWARD);
		MOTOR_setDir(MOTOR_LEFT, MOTOR_FORWARD);
	}
}
